package com.nyist.threadCase2;

import cn.hutool.core.util.IdUtil;

/**
 * 1.
 *
 * @ClassName BuyTicket
 * @Description
 * @Author xiaoyang
 * @Date 2023/04/08/16:34
 */
public class BuyTicket2 implements Runnable {
    static final Object lockObj = new Object();
    private static int ticket = 1000;

    public static void main(String[] args) {
        BuyTicket2 buyTicket = new BuyTicket2();
        Thread t1 = new Thread(buyTicket, "first,,1111");
        Thread t2 = new Thread(buyTicket, "second,,2222");
        Thread t3 = new Thread(buyTicket, "third,,3333");
        // t1.setPriority(1);
        // t2.setPriority(5);
        // t3.setPriority(9);
        t1.start();
        t2.start();
        t3.start();
    }


    @Override
    public void run() {
        // lock.lock();
        while (true) {
            synchronized (lockObj) {
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (ticket > 0) {
                    System.out.println("=====>>>currentThread: " + Thread.currentThread().getName() + " ing,by sell " + ticket + " ticket , and random: " + IdUtil.fastSimpleUUID());
                    ticket--;
                } else {
                    break;
                }
            }
        }

        // lock.unlock();
    }
}
